<?php

namespace app\index\controller;

use app\common\lib\Redis;
use app\index\model\Goods as G;

class Goods extends Common
{
    /**
     * 原点 2020年7月18日11:54:22 获取分类下的商品
     */
    public function index()
    {
        $keywords = input('keywords');
        $id = input('id');
        $category = input('category');
        $where = [];
        if ($keywords) {
            Redis::Instance()->hIncrBy('search', $keywords, 1);
            $where['title'] = ['like', '%' . $keywords . '%'];
        }
        if ($id) {
            if (input('all') == 1) {
                $where = ['category1' => $id];
            } else {
                $where = $id == 1 ? ['category1' => $id] : ['category2' => $id];
            }

        }
        if ($category) {
            $where['category1'] = $category;
        }
        $filed = '*';
        $page = input('page', 1);

        $list = G::getProduct($where, $page, 10, $filed, 'sort asc');

        if (request()->isAjax()) {
            return json(['code' => 1, 'msg' => 'success', 'result' => $list, 'page' => $page]);
        }
        $this->assign('list', $list);
        return $this->fetch();
    }


    /**
     * 原点 2020年7月20日14:15:30 搜索页面
     */
    public function search()
    {
        $hot = Redis::Instance()->hGetAll('search');
        arsort($hot);
        $this->assign('hot', $hot);
        return $this->fetch();
    }

    /**
     * @return mixed
     * 原点 2020年7月20日15:17:09 商品详情页面
     */
    public function read()
    {
        $n_time=time();
        $info = (new G())->read(input('id'), $this->user);
        $is=0;
        if($info['category1']==1){
           /*  $arr=explode(',',config('group_time'));
            $time=[
                'start'=>strtotime($arr['0']),
                'end'=>strtotime($arr['1'])
            ];
            if($n_time>=$time['start']&&$n_time<=$time['end']){
                $is=1;
            } */
            $info['new_yj']=db('order_group_parent')->where(['create_time'=>['<=',$n_time],'state'=>1,'product_id'=>$info['id']])->order('id desc')->value('new_bonus');
            // dump(Redis::Instance()->hGetAll('group_'.$info['id']));die;
            $now_g=Redis::Instance()->hGetAll('group_'.$info['id'])['inv'];
           
            $this->assign('now_g',$now_g);

        }else if($info['category1']==3){
            $arr=explode(',',config('seckill_time'));
            $time=[
                'start'=>strtotime($arr['0']),
                'end'=>strtotime($arr['1'])
            ];
            $this->assign('s_start', $time['end']-$n_time);
            $this->assign('is_s', $n_time-$time['start']);

        }

        if (empty($info)) $this->redirect('/index');
        if($info['category1']==2){
            $now_g=Redis::Instance()->hGetAll('fy_group_'.$info['id']);
            if($now_g){
                $g_all=db('order_group o')
                ->join('member m','o.uid=m.id')
                ->where(['parent'=>$now_g['parent_id']])->field('o.code,m.photo')->limit(5)->select();
                $xing = substr($g_all[0]['code'],3,4);  //获取手机号中间四位
                // $all_lottery_logs[$k]['tel'] = str_replace($xing,'****',$v['tel']);  //用****进行替换
                $g_all[0]['code']=str_replace($xing,'****',$g_all[0]['code']);
                $now_g['inv']=Redis::Instance()->lLen($info['id'].'_inv');
                $this->assign('now_g',$now_g);
                $this->assign('g_all',$g_all);
                $g_fail_time=Redis::Instance()->hGet('all_g_time',$now_g['parent_id']);

                $g_fail_time=json_decode($g_fail_time,true);
                $this->assign('g_over',$g_fail_time['over_time']-$n_time);
            }
            //获取拼团倒计时
            $next_group=Redis::Instance()->get('next_group');//下一次可以发起拼团时间
            $this->assign('g_start', $next_group-$n_time);
            //获取个人拼团倒计时
            $group_user_time=Redis::Instance()->hGet('group_u_time',$this->user['id']);
            
            $this->assign('m_start',$group_user_time-$n_time);
        }
        $this->assign('info',$info);
        $this->assign('buy_desc',db('article')->where('id=2')->value('content'));
        $this->assign('time',$time);
        $this->assign('arr',$arr);
        $this->assign('is',$is);
        return $this->fetch('details');
    }

    /**
     * 原点 2020年7月20日15:24:17 提交至订单
     */
    public function submit()
    {
        if (!$this->user) {
            return json(['code' => -3, 'msg' => '未登录']);
        }
        return (new G())->submit(input('post.'), $this->user);
    }

    public function collect()
    {
        return (new G())->collect(input('post.'), $this->user);
    }

}
